package com.rhb.mail.mail.dao;

import java.util.*;

import com.rhb.mail.mail.bean.*;
import com.rhb.util.UID;

import org.hibernate.Query;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MailDAO_HibernateImpl extends HibernateDaoSupport implements
		MailDAO {
	static Logger logger = Logger.getLogger(MailDAO_HibernateImpl.class);

	public void delete(Mail obj) {
		try {
			System.out.println("*********** delete mail: " + obj.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void erase(Mail obj) {
		try {
			System.out.println("*********** erase mail: " + obj.toString());
			this.getHibernateTemplate().delete(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Mail findByPK(String pk) {
		return (Mail) this.getHibernateTemplate().get(Mail.class, pk);
	}

	public String create(Mail obj) {
		String pk = UID.getUID();
		obj.setMailPk(pk);
		if(obj.getFatherPk()==null || "".equals(obj.getFatherPk())){
			obj.setFatherPk(pk);
		}
		for (Object it : obj.getMailItems()) {
			MailItem item = (MailItem) it;
			item.setMail(obj);
			((MailItem) it).setReceivedPk(UID.getUID());
			System.out.println("**********  the receivedPk  " + item.getReceivedPk());
		}
		try {
			this.getHibernateTemplate().save(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pk;
	}

	public void update(Mail obj) {
		try {
			for (Object it : obj.getMailItems()) {
				MailItem item = (MailItem) it;
				item.setMail(obj);
				if (item.getReceivedPk() == null
						|| "".equals(item.getReceivedPk()))
					item.setReceivedPk(UID.getUID());
			}
			this.getHibernateTemplate().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Query getQuery(String str, MailQuery quester, String orderBy) {
		StringBuffer hql = new StringBuffer(str);
		if (quester != null) {
			if (quester.getSubject() != null
					&& !"".equals(quester.getSubject())) {
				hql.append(" and subject like :subject");
			}

			if (quester.getMadePersonPK() != null
					&& !"".equals(quester.getMadePersonPK())) {
				hql.append(" and madePersonPK = :madePersonPK");
			}

			if (quester.getMadePersonName() != null
					&& !"".equals(quester.getMadePersonName())) {
				hql.append(" and madePersonName like :madePersonName");
			}
			
			if (quester.getFatherPk() != null
					&& !"".equals(quester.getFatherPk())) {
				hql.append(" and fatherPk = :fatherPk");
			}

		}
		
		if(orderBy !=null){
			hql.append(orderBy);
		}
		
		System.out.println("*************************");
		System.out.println(hql.toString());
		System.out.println("subject = " + quester.getSubject());
		System.out.println("madePersonPK = " + quester.getMadePersonPK());
		System.out.println("madePersonName = " + quester.getMadePersonName());
		System.out.println("fatherPk = " + quester.getFatherPk());
		
		Query query = this.getSession().createQuery(hql.toString());
		if (quester != null) {
			if (quester.getSubject() != null
					&& !"".equals(quester.getSubject())) {
				query.setString("subject", "%"
						+ String.valueOf(quester.getSubject()) + "%");
			}

			if (quester.getMadePersonPK() != null
					&& !"".equals(quester.getMadePersonPK())) {
				query.setString("madePersonPK", String.valueOf(quester.getMadePersonPK()));
			}

			if (quester.getMadePersonName() != null
					&& !"".equals(quester.getMadePersonName())) {
				query.setString("madePersonName", "%"
						+ String.valueOf(quester.getMadePersonName()) + "%");
			}

			if (quester.getFatherPk() != null
					&& !"".equals(quester.getFatherPk())) {
				query.setString("fatherPk", String.valueOf(quester.getFatherPk()));
			}
		}
		return query;
	}

	//******	
	private int getAllCount_findByQuery(MailQuery quester,String hql) {
		Long allcount = new Long(0);
		try {
			Query query = getQuery(hql, quester, null);
			Iterator result = query.iterate();
			if (result.hasNext()) {
				allcount = (Long) result.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allcount.intValue();
	}

	private List findByQuery(MailQuery quester, int start, int count, String hql) {
		String orderBy = " order by madeDate desc";
		try {
			Query query = getQuery(hql, quester,orderBy);
			query.setFirstResult(start);
			query.setMaxResults(count);

			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//**********
	public int getAllCount_findByQuery(MailQuery quester) {
		String hql = "select count(mail) from Mail mail where 1=1";
		return getAllCount_findByQuery(quester,hql);
	}

	public List findByQuery(MailQuery quester, int start, int count) {
		String hql = "select mail from Mail mail where 1=1";
		return findByQuery(quester,start,count,hql);
	}
	
	///---------
	public int getAllCount_findSentMailByQuery(MailQuery mq){
		String hql = "select count(mail) from Mail mail where deleted=0 and mailPk=fatherPk";
		return getAllCount_findByQuery(mq,hql);
	}
	
	public List findSentMailByQuery(MailQuery mq,int start, int count){
		String hql = "select mail from Mail mail where deleted=0 and mailPk=fatherPk";
		return findByQuery(mq,start,count,hql);
	}
	
	///---------
	public int getAllCount_findReceivedMailByQuery(MailQuery mq){
		String hql = "select count(mail) from Mail mail where mailPk=fatherPk and mail.mailPk in(select item.mail.mailPk from MailItem as item where item.deleted=0 and item.receivedPersonPk='"+ mq.getReceivedPersonPK() +"')";
		return getAllCount_findByQuery(mq,hql);
	}
	
	public List findReceivedMailByQuery(MailQuery mq,int start, int count){
		String hql = "select mail from Mail mail where mailPk=fatherPk and mail.mailPk in(select item.mail.mailPk from MailItem as item where item.deleted=0 and item.receivedPersonPk='"+ mq.getReceivedPersonPK() +"')";
		return findByQuery(mq,start,count,hql);
	}

	///---------
	public int getAllCount_findUnreadMailByQuery(MailQuery mq){
		String hql = "select count(mail) from Mail mail where mailPk=fatherPk and mail.mailPk in(select item.mail.mailPk from MailItem as item where item.deleted=0 and item.readed=0 and item.receivedPersonPk='"+ mq.getReceivedPersonPK() +"')";
		return getAllCount_findByQuery(mq,hql);
	}
	
	public List findUnreadMailByQuery(MailQuery mq,int start, int count){
		String hql = "select mail from Mail mail where mailPk=fatherPk and mail.mailPk in(select item.mail.mailPk from MailItem as item where item.deleted=0 and item.readed=0 and item.receivedPersonPk='"+ mq.getReceivedPersonPK() +"')";
		return findByQuery(mq,start,count,hql);
	}

	///---------
	public int getAllCount_findDeletedReceivedMailByQuery(MailQuery mq){
		String hql = "select count(mail) from Mail mail where mailPk=fatherPk and mail.mailPk in(select item.mail.mailPk from MailItem as item where item.deleted=1 and item.receivedPersonPk='"+ mq.getReceivedPersonPK() +"')";
		return getAllCount_findByQuery(mq,hql);
	}
	
	public List findDeletedReceivedMailByQuery(MailQuery mq,int start, int count){
		String hql = "select mail from Mail mail where mailPk=fatherPk and mail.mailPk in(select item.mail.mailPk from MailItem as item where item.deleted=1 and item.receivedPersonPk='"+ mq.getReceivedPersonPK() +"')";
		return findByQuery(mq,start,count,hql);
	}
	///---------
	public int getAllCount_findDeletedSentMailByQuery(MailQuery mq){
		String hql = "select count(mail) from Mail mail where deleted=1 and mailPk=fatherPk";
		return getAllCount_findByQuery(mq,hql);
	}
	
	public List findDeletedSentMailByQuery(MailQuery mq,int start, int count){
		String hql = "select mail from Mail mail where deleted=1 and mailPk=fatherPk";
		return findByQuery(mq,start,count,hql);
	}

	public List findMailsByFatherPk(String pk){
		String hql = "select mail from Mail mail where 1=1";
		MailQuery mq = new MailQuery();
		mq.setFatherPk(pk);
		return findByQuery(mq,0,1000,hql);
	}

}
